home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12509 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.9 KB  |  173 lines

  1. Path: news.itmel.bhp.com.au!usenet
  2. From: Chris Kuan <kuan.chris.ch@bhp.com.au>
  3. Newsgroups: comp.lang.c++,comp.sys.hp.hpux
  4. Subject: Problem combining signal() with exceptions on HP-UX (ugh!)
  5. Date: 20 Mar 1996 08:58:13 GMT
  6. Organization: BHP Information Technology
  7. Message-ID: <4iohb5$e9i@gossamer.itmel.bhp.com.au>
  8. NNTP-Posting-Host: 134.18.242.60
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
  13.  
  14. Sorry to post a whole slab of code, but it is self-contained...
  15.  
  16. I'm running HP CC on HP-UX 9, and trying to catch signals and turn them
  17. into exceptions.
  18.  
  19. The example program below behaves inconsistently - sometimes when I press 
  20. Control-C (SIGINT) within cktest(), it gets caught OK. Other times it gets caught
  21. by the catch block in main(). Other times an Abort(coredump) happens just after
  22. the cout within ckDefaultSignalHandler.
  23.  
  24. Question : how can I get signalling and exception handling to work consistently?
  25.  - and don't say, "Don't do it." It's beyond my control as to WHY this is 
  26. necessary :-( 
  27.  
  28. ==================================================================
  29. #include <unistd.h>
  30. #include <signal.h>
  31.  
  32. #include <iostream.h>
  33.  
  34. class SignalException
  35. {
  36. public:
  37.  
  38. SignalException(int signalNumber) : mSignalNumber( signalNumber){};
  39.  
  40. int SignalNumber( void){ return mSignalNumber;};
  41.  
  42. private : 
  43.  
  44. int mSignalNumber;
  45.  
  46. };
  47.  
  48.  
  49. double cktest( int ckflag);
  50.  
  51. static void ckDefaultSignalHandler(int signalNumber);
  52.  
  53. void main(void)
  54. {
  55.    int caught = 0;
  56. try
  57.    {
  58.    signal(SIGINT,    &ckDefaultSignalHandler);
  59.  
  60.    while ( caught == 0)
  61.       {
  62.  
  63.       int testflag = 1;
  64.       double result = cktest( testflag);
  65.       cout << "test() = " << result << endl;
  66.  
  67.    
  68.       cout << " main() for loop..." << endl;
  69.       for (;;)
  70.          ;
  71.      
  72.       } // while
  73.  
  74.    } // try
  75.  
  76. catch( SignalException exception)
  77.    {
  78.    if ( exception.SignalNumber() == SIGFPE)
  79.       {
  80.       cout << "\nA floating point exception occurred in main()!"
  81.            << endl;
  82.       caught = 1;
  83.       }
  84.    else
  85.       {
  86.       cout << "\nAn unexpected signal occurred in main()!"
  87.            << "\nSignal number = "
  88.            << exception.SignalNumber()
  89.            << endl;
  90.       }  
  91.    } // catch
  92.    
  93. catch(...)
  94.    {
  95.    cout << "An unexpected exception was caught in main!" << endl;
  96.    }   
  97.  
  98. cout << "Leaving main()... caught = " << caught << endl;
  99.  
  100. } // main
  101.  
  102.  
  103. double cktest( int ckflag)
  104. {
  105.  
  106. cout << "entering test()..." << endl;
  107.  
  108.  
  109. while ( ( ckflag < 3 ) )
  110.    {
  111.    cout << "ckflag = " << ++ckflag << endl;
  112.    try
  113.       {
  114.       double ckdouble1 = 1.0;
  115.       double ckdouble2 = 100.0;
  116.       double ckdouble3 = ckdouble2 / ckdouble1;
  117.  
  118.       cout << " test() for loop..." << endl;
  119.       for (;;)
  120.          ;
  121.  
  122.       return( ckdouble3);
  123.       } // try
  124.  
  125.    catch( SignalException exception)
  126.       {
  127.       if ( exception.SignalNumber() == SIGFPE)
  128.          {
  129.          cout << "\nA floating point exception occurred in test()!"
  130.          << endl;
  131.          }
  132.       else
  133.          {
  134.          cout << "\nAn unexpected signal occurred in test()!"
  135.               << "\nSignal number = "
  136.               << exception.SignalNumber()
  137.               << endl;
  138.          }  
  139.  
  140.       } // catch
  141.  
  142.    catch(...)
  143.       {
  144.       cout << "An unexpected exception was caught in test!" << endl;
  145.       }
  146.    
  147.    
  148.    } // while
  149.    
  150. cout << "leaving test()..." << endl;
  151.  
  152.  
  153. } // test
  154.  
  155. static void ckDefaultSignalHandler(int signalNumber)
  156. {
  157.     signal(signalNumber,    &ckDefaultSignalHandler);
  158.     cout << "\nckDefaultSignalHandler called. signalNumber = " << signalNumber << 
  159. endl;
  160.  
  161.     throw SignalException(signalNumber);
  162.     
  163. }
  164.  
  165.  
  166. ================================================================================
  167. Chris Kuan                                    : Email kuan.chris.ch@bhp.com.au
  168. Systems Integration Services                  : Voice +61 42 75 5657
  169. BHP Information Technology, Wollongong Region : Fax   +61 42 75 5500
  170. ================================================================================
  171.  
  172.  
  173.